

*******************************************************
*******************************************************
*************** THIS IS DO FILE 11 ********************
********* THIS FILE RUNS THE MOVER REGRESSIONS ********
*******************************************************
*******************************************************

cap log close 
cap mkdir "$output/mover"
cap mkdir "$output/log"
cap mkdir "$output/log/mover"

log using "$output/log/mover/11_regression_analysis_mover.smcl", replace 

di "This run uses code version from 2023/03/29"

quietly {


use `"$raw_input/$mover_file_size"', clear

merge m:1 payee_state_cd using `"$indices/fee_indices_medicaid.dta"', keepusing(pi*)
keep if _merge == 3
drop _merge 

merge m:1 payee_state_cd using `"$indices/cip_indices_medicaid.dta"', keepusing(*CIP*)
keep if _merge == 3
drop _merge 

//merge controls 
merge m:1 year FIPS using `"$control_var/county_controls.dta"'
keep if _merge == 3 // all data from master file merge 
drop _merge 

destring statefips, replace  
merge m:1 year statefips using `"$control_var/control_vars_state.dta"'
keep if _merge == 3 //all data from the master file merge 
drop _merge 

*******Generate Pre-Post Differences*****
sort npimedicare_id YearPost
unab controllist: mean_ma_pen mean_number_mediciad_elig mean_dual mean_unins65 mean_number_medicare_elig mean_medicare_enrol 
unab indexlist: pi_c_FE_cpt_line ???_CIP_FE_nofe ???_CIP_FE 
unab denial_indexlist: v_initial_den_CIP  

unab year_controllist: hinsva hinscare hinscaid hcovany veteran unempl med_hh_ince avg_hhc county_pop tot_active_md poverty white

foreach var of varlist `controllist' `indexlist' `indexlist_rf' `denial_indexlist' {
	
	keep if `var' != . 
	
	tempvar feevalpost feevalpre feevalpost2 feevalpre2
	
	by npimedicare_id YearPost: gegen `feevalpost' = mean(`var') if event_post
	by npimedicare_id YearPost: gegen `feevalpre' = mean(`var') if !event_post
	
	by npimedicare_id: gegen `feevalpost2' = mean(`feevalpost') 
	by npimedicare_id: gegen `feevalpre2' = mean(`feevalpre') 
	
	gen diffm`var' = `feevalpost2' - `feevalpre2' 
	
	drop `feevalpost' `feevalpre' `feevalpost2' `feevalpre2'	
}

cap drop MDsPerCapita
gen MDsPerCapita = tot_active_md/county_pop

foreach c of varlist veteran unempl poverty white {
	
	replace `c' = `c'/county_pop
}

foreach var of varlist `year_controllist' MDsPerCapita {
	
	keep if `var' != . 
	
}




local health_ins_market "c.diffmmean_ma_pen c.diffmmean_number_mediciad_elig c.diffmmean_dual c.diffmmean_unins65 c.diffmmean_number_medicare_elig c.diffmmean_medicare_enrol" 
local health_ins_market_loop "diffmmean_ma_pen diffmmean_number_mediciad_elig diffmmean_dual diffmmean_unins65 diffmmean_number_medicare_elig diffmmean_medicare_enrol" 

foreach var of local health_ins_market_loop {
	
	gen `var'_p = `var'*event_post 
	
}

unab health_ins_market_post: diffmmean_ma_pen_p diffmmean_number_mediciad_elig_p diffmmean_dual_p diffmmean_unins65_p diffmmean_number_medicare_elig_p diffmmean_medicare_enrol_p

unab health_ins_market_year: hinscaid hcovany hinscare avg_hhc 
unab health_prov_year: tot_active_md MDsPerCapita
unab ses_year: unempl white county_pop veteran poverty med_hh_ince


xtset npimedicare_id

local loss_denial v_initial_den_CIP`v'
local loss lev_CIP_FE`v'
local loss_heck lev_CIP_FE`v'_nofe
local fee pi_c`v'_FE_cpt_line

gen diffm`loss_denial'_post = diffm`loss_denial'*event_post
gen diffm`loss'_post = diffm`loss'*event_post
gen diffm`loss_heck'_post = diffm`loss_heck'*event_post
gen diffm`fee'_post = diffm`fee'*event_post

local loss_name 1.event_post#c.diffm`loss' diffmCIP diffm`loss'_post diffmCIP 1.event_post#c.diffm`loss_heck' diffmCIP diffm`loss_heck'_post diffmCIP 
local fee_name 1.event_post#c.diffm`fee' diffmFee diffm`fee'_post diffmFee	

}


*********************************************************************
********** SAMPLE SELECTION AND COUNT NUMBER OF PHYSICIANS **********
*********************************************************************
gen accept_medicare = 1 if medicare == "Y"
replace accept_medicare = 0 if medicare == "N"
replace accept_medicare = . if acceptinsurance == . 
bys npimedicare_id: gegen mean_medicare = mean(accept_medicare)
gen capacity = 1 - mean_medicare
** select only physicians accepting Medicare
****Select Sample 
local sampleselect   "(nummoves==1 | nummoves==0) & ( eventtime >=-3 & eventtime <=4 | eventtime == . ) & capacity == 0 " 
****
preserve 
keep if `sampleselect'
drop if acceptinsurance == . 
*Determine # of Physicians 
gen physind = 0
bys npimedicare_id: replace physind = 1 if _n == 1 
gegen phys_count = sum(physind)
local physcount : di %7.0fc phys_count
restore 
****	
	
************************************************************************	
****************** COLUMN 1 ** No selection, Restricted sample OLS
areg acceptinsurance  (c.diffm`fee' c.diffm`loss' `health_ins_market')##(i.event_post) `health_ins_market_year' `health_prov_year' `ses_year' ib2012.year if `sampleselect', absorb(npimedicare_id) vce(cluster paystateid)
		
	estadd local pys_number "`physcount'"
	estadd local sel_cor_index "No"
	estadd local phys_fe_index "Yes"
	estadd local instrument "OLS"
	estadd local Phys_FE "Yes"
	estadd local Controls "Yes"
	estadd local Sample "Yes"
	eststo mainmovers1
	

************************************************************************	
****************** COLUMN 2 ** No selection, Restricted sample IV
ivreghdfe acceptinsurance  diffm`fee'_post event_post (diffm`loss'_post = diffm`loss_denial'_post) `health_ins_market_post' `health_ins_market_year' `health_prov_year' `ses_year' ib2012.year if `sampleselect', absorb(npimedicare_id) cluster(paystateid) first
	
	estadd local pys_number "`physcount'"
	estadd local sel_cor_index "No"
	estadd local phys_fe_index "Yes"
	estadd local instrument "2SLS"
	estadd local Phys_FE "Yes"
	estadd local Controls "Yes"
	estadd local Sample "Yes"
	eststo mainmovers2


	
************************************************************************
****************** COLUMN 3 ** Selection Correction, Restricted sample OLS
areg acceptinsurance  diffm`fee'_post event_post diffm`loss_heck'_post   `health_ins_market_post' `health_ins_market_year' `health_prov_year' `ses_year' ib2012.year if `sampleselect', absorb(npimedicare_id) vce(cluster paystateid)

	estadd local pys_number "`physcount'"
	estadd local sel_cor_index "Yes"
	estadd local phys_fe_index "No"
	estadd local instrument "OLS"
	estadd local Phys_FE "Yes"
	estadd local Controls "Yes"
	estadd local Sample "Yes"
	eststo mainmovers3



************************************************************************
****************** COLUMN 4 ** Selection Correction, Restricted sample IV
ivreghdfe acceptinsurance  diffm`fee'_post event_post (diffm`loss_heck'_post  = diffm`loss_denial'_post ) `health_ins_market_post' `health_ins_market_year' `health_prov_year' `ses_year' ib2012.year if `sampleselect', absorb(npimedicare_id) cluster(paystateid) first
	
	estadd local pys_number "`physcount'"
	estadd local sel_cor_index "Yes"
	estadd local phys_fe_index "No"
	estadd local instrument "2SLS"
	estadd local Phys_FE "Yes"
	estadd local Controls "Yes"
	estadd local Sample "Yes"
	eststo mainmovers4
	
	
//first stage corresponding to column 2
areg diffm`loss'_post diffm`loss_denial'_post  diffm`fee'_post event_post ib2012.year `health_ins_market_post' `health_ins_market_year' `health_prov_year' `ses_year' if e(sample), absorb(npimedicare_id) cluster(paystateid)
	estadd local pys_number "`physcount'"
	estadd local sel_cor_index "Yes"
	estadd local phys_fe_index "No"
	estadd local Phys_FE "Yes"
	estadd local Controls "Yes"
	estadd local Sample "Yes"
	eststo mainmoversFS2
	
//first stage corresponding to column 4
areg diffm`loss_heck'_post diffm`loss_denial'_post  diffm`fee'_post event_post ib2012.year `health_ins_market_post' `health_ins_market_year' `health_prov_year' `ses_year' if e(sample), absorb(npimedicare_id) cluster(paystateid)
	estadd local pys_number "`physcount'"
	estadd local sel_cor_index "Yes"
	estadd local phys_fe_index "No"
	estadd local Phys_FE "Yes"
	estadd local Controls "Yes"
	estadd local Sample "Yes"
	eststo mainmoversFS4

	

	
************************************************************************	
************************************************************************	
*************** GENERATE .TEX TABLE

#delimit;
	
	esttab mainmovers1 mainmovers2 mainmovers3 mainmovers4 using "$paperinputs/mover_main.tex" ,  replace substitute(_ \_)
	nomtitles booktabs
	mgroups("Accept Medicaid Patients?", pattern(1 0 0 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))
	rename(`loss_name' `fee_name')
	keep(diffmCIP diffmFee) 
	order(diffmCIP diffmFee)
	coeflabels(diffmFee "Post-move $\times \Delta \log \pi$ index"
			   diffmCIP "Post-move $\times \Delta \tau$ index"

	)
	se b(4) se(4 ) stats(instrument Sample pys_number N Phys_FE Controls phys_fe_index  sel_cor_index , fmt(%7.0fc %9.0fc) labels("\footnotesize{Estimator}" "\footnotesize{Subsample Accepting Medicare}" "\footnotesize{N.~Physicians}" "\footnotesize{N.~Physicians-Years}" "\footnotesize{Physician FE}" "\footnotesize{Controls}"  "\midrule  $\tau$ index: \\ \multicolumn{1}{r}{\footnotesize{Physician FE}}" "\multicolumn{1}{r}{\footnotesize{Selection Correction}}" ))
	starlevels(* 0.10 ** 0.05 *** 0.01)  label varwidth(20) nonotes;
	
	esttab mainmoversFS2 mainmoversFS4 using "$paperinputs/mover_mainFS.tex" ,  replace substitute(_ \_)
	nomtitles booktabs
	mgroups("Post-move $\times \Delta \tau$ index", pattern(1 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))
	keep(diffm`loss_denial'_post diffm`fee'_post)
	order(diffm`loss_denial'_post diffm`fee'_post)
	coeflabels(diffm`fee'_post "Post-move $\times \Delta \log \pi$ index"
			   diffm`loss_denial'_post "Post-move $\times \Delta$ denial rate index"
	)
	se b(4) se(4 ) stats(Sample pys_number N Phys_FE Controls phys_fe_index  sel_cor_index , fmt(%7.0fc %9.0fc) labels("\footnotesize{Subsample Accepting Medicare}" "\footnotesize{N.~Physicians}" "\footnotesize{N.~Physicians-Years}" "\footnotesize{Physician FE}" "\footnotesize{Controls}"  "\midrule  $\tau$ index: \\ \multicolumn{1}{r}{\footnotesize{Physician FE}}" "\multicolumn{1}{r}{\footnotesize{Selection Correction}}" ))
	starlevels(* 0.10 ** 0.05 *** 0.01)  label varwidth(20) nonotes;
	
	
#delimit cr
	
		
	
	

	
	
	
	
	
	
	
*******************************************************
*******************************************************
********************** GRAPHS *************************
*******************************************************
*******************************************************	
set scheme s2color 

gen year_ante_one = 0 
gen year_ante_two = 0
gen year_post_one = 0 
gen year_post_two = 0 
gen year_post_three = 0 
gen year_post_four = 0 

replace year_ante_one = 1 if YearAnte == 1 
replace year_ante_two = 1 if YearAnte == 2
replace year_post_one = 1 if YearPost == 1 
replace year_post_two = 1 if YearPost == 2
replace year_post_three = 1 if YearPost == 3 
replace year_post_four = 1 if YearPost == 4


gen diffmlev_CIP_p_one = diffmlev_CIP_FE*year_post_one
gen diffmlev_CIP_p_two = diffmlev_CIP_FE*year_post_two
gen diffmlev_CIP_p_three = diffmlev_CIP_FE*year_post_three
gen diffmlev_CIP_p_four = diffmlev_CIP_FE*year_post_four
gen diffmlev_CIP_a_one = diffmlev_CIP_FE*year_ante_one
gen diffmlev_CIP_a_two = diffmlev_CIP_FE*year_ante_two

gen diffmlev_CIP_heck_p_one = diffmlev_CIP_FE_nofe*year_post_one
gen diffmlev_CIP_heck_p_two = diffmlev_CIP_FE_nofe*year_post_two
gen diffmlev_CIP_heck_p_three = diffmlev_CIP_FE_nofe*year_post_three
gen diffmlev_CIP_heck_p_four = diffmlev_CIP_FE_nofe*year_post_four
gen diffmlev_CIP_heck_a_one = diffmlev_CIP_FE_nofe*year_ante_one
gen diffmlev_CIP_heck_a_two = diffmlev_CIP_FE_nofe*year_ante_two

gen diffmfee_p_one = diffmpi_c_FE_cpt_line*year_post_one
gen diffmfee_p_two = diffmpi_c_FE_cpt_line*year_post_two
gen diffmfee_p_three = diffmpi_c_FE_cpt_line*year_post_three
gen diffmfee_p_four = diffmpi_c_FE_cpt_line*year_post_four
gen diffmfee_a_one = diffmpi_c_FE_cpt_line*year_ante_one
gen diffmfee_a_two = diffmpi_c_FE_cpt_line*year_ante_two

gen diffm_denial_p_one = diffmv_initial_den_CIP*year_post_one
gen diffm_denial_p_two = diffmv_initial_den_CIP*year_post_two
gen diffm_denial_p_three = diffmv_initial_den_CIP*year_post_three
gen diffm_denial_p_four = diffmv_initial_den_CIP*year_post_four
gen diffm_denial_a_one = diffmv_initial_den_CIP*year_ante_one
gen diffm_denial_a_two = diffmv_initial_den_CIP*year_ante_two

eststo clear 


//no controls 
eststo clear 
ivreghdfe acceptinsurance (diffmlev_CIP_a_one diffmlev_CIP_a_two diffmlev_CIP_p_one diffmlev_CIP_p_two diffmlev_CIP_p_three diffmlev_CIP_p_four = diffm_denial_a_one diffm_denial_a_two diffm_denial_p_one diffm_denial_p_two diffm_denial_p_three diffm_denial_p_four)  diffmfee_a_one diffmfee_a_two diffmfee_p_one diffmfee_p_two diffmfee_p_three diffmfee_p_four i(0/2)b0.YearAnte i.YearPost ib2012.year if `sampleselect', absorb(npimedicare_id)  cluster(paystateid)  nocollin partial(i(0/2)b0.YearAnte ib2012.year)

matrix MatB = e(b)
matrix MatV = e(V)
matrix MatB[1,13] = 0
matrix MatV[13,13] = 0

cap graph drop cip_iv
coefplot matrix(MatB), vertical keep(1.YearPost diffmlev_CIP_a_one diffmlev_CIP_a_two diffmlev_CIP_p_one diffmlev_CIP_p_two diffmlev_CIP_p_three diffmlev_CIP_p_four year_zero) yline(0) v(MatV) levels(95)  rename(diffmlev_CIP_a_one =  "-1" diffmlev_CIP_a_two = "-2" 1.YearPost = "0"  diffmlev_CIP_p_one = "1" diffmlev_CIP_p_two = "2" diffmlev_CIP_p_three = "3" diffmlev_CIP_p_four = "4") order("-2" "-1" "0" "1" "2" "3" "4") xti("Years since Move") yti("Impact on Medicaid Acceptance", margin(small)) ciopts(lwidth(medthin) lcolor(ebblue)) msize(small) msymbol(D) mcolor(navy) name(cip_iv) aspectratio(1) xsize(5) ysize(5) graphregion(color(white) margin(small)) ylab(, nogrid)
graph export `"$paperinputs/MoversEventStudy_cip_col2_nocontrols.pdf"', replace
graph export `"$paperinputs/MoversEventStudy_cip_col2_nocontrols.png"', replace


cap graph drop fee_iv
coefplot matrix(MatB), vertical keep(1.YearPost diffmfee_a_one diffmfee_a_two diffmfee_p_one diffmfee_p_two diffmfee_p_three diffmfee_p_four year_zero) yline(0) v(MatV) levels(95)  rename(diffmfee_a_one =  "-1" diffmfee_a_two = "-2" 1.YearPost = "0" diffmfee_p_one = "1" diffmfee_p_two = "2" diffmfee_p_three = "3" diffmfee_p_four = "4") order("-2" "-1" "0" "1" "2" "3" "4") xti("Years since Move") yti("Impact on Medicaid Acceptance", margin(small)) ciopts(lwidth(medthin) lcolor(ebblue)) msize(small) msymbol(D) mcolor(navy) name(fee_iv) aspectratio(1) xsize(5) ysize(5) graphregion(color(white) margin(small)) ylab(, nogrid)
graph export `"$paperinputs/MoversEventStudy_fee_col2_nocontrols.pdf"', replace
graph export `"$paperinputs/MoversEventStudy_fee_col2_nocontrols.png"', replace




log close 



	
	
